#include<iostream>
using namespace std;
class ListNode {
public:
	string name;
	int number;
	ListNode* next;
	ListNode() {
		next = NULL;
	}
};
class List {
public:
	ListNode* head;
	int len;
	List() {
		len = 0;
		head = new ListNode;
	}
	~List() {
		ListNode* p = head;
		while (p != NULL) {
			head = head->next;
			delete p;
			p = head;
		}
		len = 0;
	}
	void Init() {
		ListNode* q = head;
		for (int i = 101; i <= 120; i++) {
			ListNode* p = new ListNode;
			p->number = i;
			q->next = p;
			q = p;
		}
		len = 20;
	}
	void free(int number) {
		ListNode* p = head;
		while (p->next != NULL) {
			if (p->next->number == number) {
				p->next = p->next->next;
				len--;
				break;
			}
			p = p->next;
		}
	}
	void insert(string name_, int _number) {
		if (head->next == NULL) {
			ListNode* p = new ListNode;
			p->name = name_;
			p->number = _number;
			head->next = p;
			len++;
		}
		else {
			ListNode* p = head->next;
			ListNode* q = new ListNode;
			q->name = name_;
			q->number = _number;
			if (p->number > q->number) {
				q->next = p;
				head->next = q;
				goto A;
			}
			while (p != NULL) {
				if (q->number > p->number) {
					if (p->next == NULL) {
						p->next = q;
						break;
					}
					else {
						if (p->next->number > q->number) {
							q->next = p->next;
							p->next = q;
							break;
						}
					}
					p = p->next;
				}

			}
		A:
			len++;
		}
	}
	void return_(int number,List&L) {
		ListNode* p = head;
		while (p->next != NULL) {
			if (p->next->number == number) {
				p->next = p->next->next;
				len--;
				ListNode* q = L.head;
				while (q->next != NULL) {
					q = q->next;
				}
				ListNode* ptr = new ListNode;
				ptr->number = number;
				q->next = ptr;
				L.len++;
				break;
			}
			p = p->next;
		}
	}
	void assign_(string name,List&L) {
		ListNode* p = L.head->next;
		L.head->next = p->next;
		L.len--;
		insert(name, p->number);
	}
	void print() {
		ListNode* p = head->next;
		while (p != NULL) {
			cout << p->name << ' ' << p->number << endl;
			p = p->next;
		}
	}
	void display_free() {
		ListNode* p = head->next;
		while (p->next != NULL) {
			cout << p->number << '-';
			p = p->next;
		}
		cout << p->number << endl;
	}
	void display_used() {
		ListNode* p = head->next;
		while (p->next != NULL) {
			cout << p->name << '(' << p->number << ")-";
			p = p->next;
		}
		cout << p->name << '(' << p->number << ')' << endl;
	}
};
int main()
{
	List L_use;
	List L_free;
	L_free.Init();
	int n;
	cin >> n;
	string name;
	int number;
	for (int i = 0; i < n; i++) {
		cin >> name >> number;
		L_use.insert(name, number);
		L_free.free(number);
	}
	//L.print();
	string str;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> str;
		if (str == "assign") {
			cin >> name;
			L_use.assign_(name, L_free);
		}
		else if (str == "return") {
			cin >> number;
			L_use.return_(number, L_free);
		}
		else if (str == "display_used") {
			L_use.display_used();
		}
		else if (str == "display_free") {
			L_free.display_free();
		}
	}
	return 0;
}